package cn.wolfcode.web.controller;


import cn.wolfcode.domain.Activity;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IActivityItemService;
import cn.wolfcode.service.IActivityService;
import cn.wolfcode.util.RequiredPermission;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/activity")
public class ActivityController {

    @Autowired
    private IActivityService activityService;

    private IActivityItemService service;


    @RequiredPermission(name = "投票活动页面", expression = "activity:list")
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") QueryObject qo) {
        PageInfo<Activity> pageInfo = activityService.query(qo);
        model.addAttribute("pageInfo", pageInfo);
        return "activity/list";
    }


    @RequiredPermission(name = "投票活动删除", expression = "activity:delete")
    @RequestMapping("/delete")
    public String delete(Long id) {
        if (id != null) {
            activityService.delete(id);
        }
        return "redirect:/activity/list";
    }


    @RequiredPermission(name = "投票活动新增/编辑", expression = "activity:saveOrUpdate")
    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Activity activity) {
        if (activity.getId() == null) {
            activityService.save(activity);
        } else {
            activityService.update(activity);
        }
        return "redirect:/activity/list";
    }


    @RequiredPermission(name = "投票活动新增/编辑", expression = "activity:saveOrUpdate")
    @RequestMapping("/input")
    public String input(Long id, Model model) {
        if (id != null) {
            model.addAttribute("activity", activityService.get(id));
        }
        return "vote";
    }

     @RequestMapping("/vote")
    public String vote(Model model,Long id){
        model.addAttribute("content",activityService.getContent(id));
        model.addAttribute("vote",activityService.get(id));
        return "activity/vote";
    }

}
